// pages/cases/list.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["风格", "户型", "面积"],
    tabkeys: ['style','house_type','mianji'],
    tabs_nav:[
      ['不限', '简约', '中式', '欧式', '美式', '田园', '复古', '混搭', '地中海', '北欧', '其他'],
      ['不限', '一居室', '二居室', '三居室', '四居室', '复式', '其他'],
      ['不限', '70㎡以下', '70-80㎡', '80-100㎡', '100-120㎡', '120-150㎡', '150㎡以上','其他']
    ],
    tabs_key:[[],[0,70],[70,80],[80,100],[100,120],[120,150],[150,1000]],
    tabselected:[0,0,0],
    activeIndex: -1,
    list: [],
    count: 4,//每页记录数
    page: 1,
    listcount: 1,
    likestr:'',
    citys:[]
  },
  callphonenum() {
    wx.makePhoneCall({
      phoneNumber: '4001529288'
    })
  },

  loadswiper() {
    api.get({
      url: 'home/slides/2',
      success: data => {
        if (data.code == 1) {
          this.setData({
            movies: data.data[0].items
          })
        }
      }
    });
  },

  tabClick: function (e) {
    this.setData({
      activeIndex: e.currentTarget.id
    });
  },

  //加载家装案例
  loadingData(page, count) {
    api.get({
      url: '/portal/lists/getCategoryPostLists',
      data: {
        category_id: 2,
        field: 'post.id,post.post_title,post.more',
        page: page + ',' + count,
        order: '-post.id',
        like: this.data.likestr
      },
      success: data => {
        if (data.code == '1') {
          let list=[];
          let datalist=data.data.list;
          if (this.data.tabselected[2]!=0){
              for(var i=0;i<datalist.length;i++){
                if (datalist[i].more.mianji > this.data.tabs_key[this.data.tabselected[2]][0]&&
                  datalist[i].more.mianji <= this.data.tabs_key[this.data.tabselected[2]][1])
                  {
                  list.push(datalist[i]);
                  }
              }
          }else{
            list = datalist;
          }
          this.setData({
            list: this.data.list.concat(list),
            page: page,
            listcount: list.length
          })
        }
      }
    })
  },
  loadmore(e) {
    let page = this.data.page + 1;
    this.loadingData(page, this.data.count);
  },

  selectitem(e){
    let tabselected = this.data.tabselected;
    if (e.currentTarget.dataset.id != tabselected[this.data.activeIndex]){
      tabselected[this.data.activeIndex] = e.currentTarget.dataset.id;
      this.setData({
        tabselected: tabselected
      })
      let likestr='';
      for(var i=0;i<tabselected.length-1;i++){
        if (tabselected[i]==0){
          likestr+='%,';
        }else{
          tabselected[i]--;
          likestr += '"' + this.data.tabkeys[i] + '":"' + tabselected[i] + '",'
        }
      }
      console.log(likestr)
      likestr = encodeURIComponent(likestr.substr(0,likestr.length-1));
      this.setData({
        list: [],
        likestr: likestr
      })
      this.loadingData(1, this.data.count);
    }
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadswiper();
    this.loadingData(1, this.data.count);
    this.dialog = this.selectComponent("#mydialog");
  },

  formSubmit(e) {
    console.log(e)
    //提交表单数据
    api.post({
      url: 'portal/diyform/insert',
      data: e.detail.value,
      success: data => {
        if (data.code == "1") {
          wx.showToast({ title: '报名成功', icon: 'success' })
          this.dialog.hideModal();
        }
        if (data.code == "0") {
          wx.showToast({ title: data.msg, icon: 'none' })
        }
      }
    })
  },

  yuyuefunc(e) {
    wx.switchTab({
      url: '/pages/index/yuyue',
    })
  },
  _hideModal() {
    this.dialog.hideModal();
  },

  loadcitys() {
    api.get({
      url: 'home/attribute/11',
      success: data => {
        if (data.code == 1) {
          this.dialog.setparam(data.data[0].items);
          this.setData({
            citys: data.data[0].items
          })
        }
      }
    });
  },
  bindCitysChange(e) {
    this.setData({
      index: e.detail.value
    })
  },
  onslideup(){
    this.loadcitys();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})